<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串扩展</title>
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>
<body>
<script type="text/javascript">
    {
        /**确定一个字符串是否包含在另一个字符串中*/
        let s = 'Hello world!';
        console.log(s.startsWith('Hello')); // true
        console.log(s.endsWith('!')); // true
        console.log(s.includes('o')); // true
        /*第二个参数，表示开始搜索的位置*/
        console.log(s.startsWith('world', 6)); // true
        console.log(s.endsWith('Hello', 5)); // true
        console.log(s.includes('Hello', 6)); // false
    }
    {
        /** repeat方法返回一个新字符串，表示将原字符串重复n次。 */
        let str = 'abc';
        console.log(str.repeat(4)); // abcabcabcabc
        /*参数如果是小数，会被取整。*/
        console.log('na'.repeat(2.9)); // nana
    }
    {
        /** 字符串补全长度的功能*/
        console.log('x'.padStart(5, 'ab')); // 'ababx'
        console.log('x'.padStart(4, 'ab')); // 'abax'

        let date = new Date();
        let day = date.getDate();
        console.log(day);
        console.log(('' + day).padStart(2, '0'));

        console.log('x'.padEnd(5, 'ab')); // 'xabab'
        console.log('x'.padEnd(4, 'ab')); // 'xaba'
    }
    {
        /**模版字符串*/
        let name = "Bob", time = "today";
        console.log(`https://www.baidu.com?name=${name}&time=${time}`);
    }
    {
        const tmpl = adds => `
        <table>
            ${adds.map(addr => `
                <tr><td>${addr.first}</td></tr>
                <tr><td>${addr.last}</td></tr>
            `).join('')}
        </table>
        `;
        const data = [
            {first: '<Jane>', last: 'Bond'},
            {first: 'Lars', last: '<Croft>'},
        ];
        console.log(tmpl(data));
    }
    {
        console.log(String.raw`Hi\n${2 + 3}!`);/*Hi\n5!*/
        console.log(String.raw`Hi\u000A!`);
    }
</script>
</body>
</html>